/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is lutsko.com code.
 *
 * The Initial Developer of the Original Code is  James Lutsko 
 * (jim@lutsko.com http://www.lutsko.com).
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

package autoschedule.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.JTable;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import autoschedule.AutoScheduleDoc;
import autoschedule.League;
import autoteam.tables.TableSorter;

public class LeaguePanel extends JPanel
{
  final SpinnerNumberModel teamModel = new SpinnerNumberModel(0, // initial
                                                                 // value
      0, // min
      1000, // max
      1); // step
  final SpinnerNumberModel gameModel = new SpinnerNumberModel(10, // initial
                                                                  // value
      0, // min
      1000, // max
      1); // step

  final SpinnerNumberModel gamesWeekModel = new SpinnerNumberModel(1, // initial
                                                                      // value
      0, // min
      10, // max
      0.1); // step

  final SpinnerNumberModel hrsModel = new SpinnerNumberModel(1, // initial value
      0, // min
      24, // max
      0.25); // step

  League theLeague = null;

  FacilityAssignmentModel model = null;
  JTable table = null;
  JLabel theLabel;

  void setLeague(League l)
  {
    theLeague = l;
    if (theLeague != null)
      theLabel.setText("<html><h2>Properties of League: " + l.getName()
          + "</h2></html>");
    else
      theLabel.setText("<html><h2>Properties of League: <none></h2></html>");

    if (l != null)
    {
      teamModel.setValue(l.getTeams());
      gameModel.setValue(l.getGamesPerTeam());
      gamesWeekModel.setValue(l.getGamesPerWeek());
      hrsModel.setValue(l.getHrsPerGame());
    }
    if (table.isEditing())
    {
      table.getCellEditor().stopCellEditing();
    }
    model.setCurrentLeague(l);
    model.fireTableDataChanged();
  }

  public void setDoc(AutoScheduleDoc _doc)
  {
    model.setDoc(_doc);
  }

  public LeaguePanel(AutoScheduleDoc doc)
  {

    model = new FacilityAssignmentModel(doc);

    // create table

    TableSorter sorter = new TableSorter(model);
    table = new JTable(sorter);
    // sorter.addMouseListenerToHeaderInTable(table);
    sorter.setTableHeader(table.getTableHeader());

    // set table properties
    table.setRowSelectionAllowed(true);

    JScrollPane tableScrollPane = new JScrollPane(table);

    // For layout purposes, put the buttons in a separate panel
    JPanel buttonFlowPanel = new JPanel();

    JSpinner spinner = addLabeledSpinner("Teams", teamModel, buttonFlowPanel);
    spinner.addChangeListener(new ChangeListener()
    {
      public void stateChanged(ChangeEvent e)
      {
        theLeague.setTeams((Integer) teamModel.getNumber());
      }
    });

    spinner = addLabeledSpinner("Games/Team", gameModel, buttonFlowPanel);
    spinner.addChangeListener(new ChangeListener()
    {
      public void stateChanged(ChangeEvent e)
      {
        theLeague.setGamesPerTeam((Integer) gameModel.getNumber());
      }
    });

    spinner = addLabeledSpinner("Games/Week", gamesWeekModel, buttonFlowPanel);
    spinner.addChangeListener(new ChangeListener()
    {
      public void stateChanged(ChangeEvent e)
      {
        theLeague.setGamesPerWeek((Double) gamesWeekModel.getNumber());
      }
    });

    spinner = addLabeledSpinner("Hrs/Game", hrsModel, buttonFlowPanel);
    spinner.addChangeListener(new ChangeListener()
    {
      public void stateChanged(ChangeEvent e)
      {
        theLeague.setHrsPerGame((Double) hrsModel.getNumber());
      }
    });

    theLabel = new JLabel("Properties of League: <none>", JLabel.CENTER);

    JPanel buttonPanel = new JPanel(new BorderLayout());
    buttonPanel.add(theLabel, BorderLayout.NORTH);
    buttonPanel.add(buttonFlowPanel, BorderLayout.CENTER);
    buttonPanel.add(Box.createRigidArea(new Dimension(10, 25)),
        BorderLayout.SOUTH);

    // the tablepanel for the table
    // a box holding a line, the label and the table
    JPanel tablePanel = new JPanel();
    tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.Y_AXIS));
    tablePanel.add(new JSeparator(SwingConstants.HORIZONTAL));

    JLabel label = new JLabel(
        "<html><h2>League Facility Requirements</h2></html>", JLabel.CENTER)
    {
      public float getAlignmentX()
      {
        return Component.CENTER_ALIGNMENT;
      }
    };
    // label.setHorizontalTextPosition(label.CENTER);
    tablePanel.add(label);
    tablePanel.add(tableScrollPane);

    // Lay out the tablepanel.
    JPanel containerPanel = new JPanel(new BorderLayout());
    containerPanel.add(buttonPanel, BorderLayout.NORTH);
    containerPanel.add(tablePanel, BorderLayout.CENTER);
    this.add(containerPanel);

    /*
     * buttonPanel.setBackground(Color.white);
     * buttonFlowPanel.setBackground(Color.white);
     * tablePanel.setBackground(Color.white); table.setBackground(Color.white);
     * tableScrollPane.setBackground(Color.white);
     * tablePanel.setBackground(Color.white); this.setBackground(Color.white);
     */
  }

  JSpinner addLabeledSpinner(String s, SpinnerModel model, JPanel panel)
  {
    JLabel label = new JLabel(s);
    label.setHorizontalAlignment(SwingConstants.TRAILING);

    JSpinner spinner = new JSpinner(model)
    {
      public Dimension getMaximumSize()
      {
        Dimension d = getPreferredSize();
        return d;
      }
    };
    label.setLabelFor(spinner);
    panel.add(label);
    panel.add(spinner);

    return spinner;
  }

  class myTextField extends JFormattedTextField
  {
    public myTextField()
    {
      super();
      setColumns(8);
    }

    public Dimension getMaximumSize()
    {
      Dimension d = getPreferredSize();
      return d;
    }
  }

}
